package com.fs.robot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fs.api.domain.vo.RobotVO;
import com.fs.api.enumeration.RobotStatus;
import com.fs.common.domain.PageVO;
import com.fs.common.domain.Result;
import com.fs.common.exception.BusinessException;
import com.fs.common.utils.CollUtils;
import com.fs.robot.domain.dto.RobotCreateDTO;
import com.fs.robot.domain.po.Robot;
import com.fs.robot.domain.vo.BatteryDistributionVO;
import com.fs.robot.domain.vo.RobotDetailVO;
import com.fs.robot.domain.vo.RobotOverviewVO;
import com.fs.robot.domain.vo.RobotStatusDistributionVO;
import com.fs.robot.service.IRobotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "机器人相关接口")
@RestController
@RequestMapping("/robots")
@RequiredArgsConstructor
@Slf4j
public class RobotController {
    private final IRobotService robotService;

    @ApiOperation("获取机器人列表")
    @GetMapping("/page")
    public Result getRobotsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.ok(robotService.getRobotsPage(pageNum, pageSize));
    }

    @ApiOperation("获取机器人详情")
    @GetMapping("/{robotId}/status")
    public Result getRobotStatus(@PathVariable Long robotId) {
        return Result.ok(robotService.getRobotStatus(robotId));
    }

    @ApiOperation("获取机器人正在处理的警情id")
    @GetMapping("/{robotId}/alerts")
    public Result getAlertIdsByRobotId(@PathVariable("robotId") Long robotId) {
        return Result.ok(robotService.getAlertIdsByRobotId(robotId));
    }

    @ApiOperation("创建机器人")
    @PostMapping
    public Result createRobot(@RequestBody RobotCreateDTO robotCreateDTO) {
        robotService.createRobot(robotCreateDTO);
        return Result.ok();
    }

    @ApiOperation("获取一众机器人详情")
    @GetMapping("/batch")
    public Result getRobotsByIds(@RequestParam("ids") List<Long> robotIds) {
        return Result.ok(robotService.getRobotsByIds(robotIds));
    }

    @ApiOperation("获取一众低电量机器人详情")
    @GetMapping("/low-battery")
    public Result getLowBatteryRobots() {
        return Result.ok(robotService.getLowBatteryRobots());
    }

    @ApiOperation("获取机器人概览")
    @GetMapping("/overview")
    public Result getRobotOverview() {
        return Result.ok(robotService.getRobotOverview());
    }

    @ApiOperation("获取机器人数量")
    @GetMapping("/count")
    public Result<Integer> getRobotCount(
            @RequestParam(required = false) RobotStatus status,
            @RequestParam(required = false) Integer batteryThreshold) {
        return Result.ok(robotService.getRobotCount(status, batteryThreshold));
    }

    @ApiOperation("获取机器人状态分布")
    @GetMapping("/status-distribution")
    public Result getRobotStatusDistribution() {
        return Result.ok(robotService.getRobotStatusDistribution());
    }

    @ApiOperation("获取电量分布")
    @GetMapping("/battery-distribution")
    public Result getBatteryDistribution() {
        return Result.ok(robotService.getBatteryDistribution());
    }

    @ApiOperation("获取待机机器人信息")
    @GetMapping("/online")
    public Result getIdleRobots(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name) {
        return Result.ok(robotService.queryIdleRobots(page, size, name));
    }

    @ApiOperation("更新机器人状态")
    @PutMapping("/{id}/status")
    public Result updateRobotStatus(
            @PathVariable("id") Long id,
            @RequestParam("status") String status) {
        robotService.updateRobotStatus(id, RobotStatus.fromValue(status));
        return Result.ok();
    }

    @ApiOperation("机器人充电")
    @PutMapping("/charge/{id}")
    public Result chargeRobot(
            @PathVariable Long id, @RequestParam Integer batteryLevel) {
        robotService.chargeRobot(id, batteryLevel);
        return Result.ok();
    }

    @ApiOperation("改变机器人位置")
    @PutMapping("/{id}/location")
    public Result updateRobotLocation(@PathVariable("id") Long robotId, @RequestBody String location){
        robotService.updateRobotLocation(robotId, location);
        return Result.ok();
    }
}
